package sn.ugb.cours.zd.ejb.sessions.facades.commons.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import sn.ugb.cours.zd.ejb.sessions.facades.commons.IFacade;
/**
 * 
 * @author zakaria
 *
 * @param <T>
 */
public abstract class AbstractFacade<T> implements IFacade<T> {
	private Class<T> entityClass;

	@PersistenceContext
	protected EntityManager em = null;

	public AbstractFacade(Class<T> entityClass) {
		this.entityClass = entityClass;
	}

	public void create(T entity) {
		getEntityManager().persist(entity);
	}

	public void edit(T entity) {
		getEntityManager().merge(entity);
	}

	public void remove(T entity) {
		getEntityManager().remove(getEntityManager().merge(entity));
	}

	public T find(Object id) {
		return getEntityManager().find(entityClass, id);
	}

	public abstract List<T> findAll();

	public EntityManager getEntityManager() {
		return em;
	}
}
